home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / c2man-2.0pl33.lha / c2man-2.0 / enum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-14  |  3.8 KB  |  180 lines

  1. /* $Id: enum.c,v 2.0.1.1 1993/08/31 05:07:21 greyham Exp $
  2.  * enumerator operations
  3.  */
  4. #include "c2man.h"
  5. #include "strconcat.h"
  6. #include "enum.h"
  7. #include "manpage.h"
  8.  
  9. SymbolTable *enum_table;    /* enum symbol table */
  10.  
  11. /* we have to keep a list of EnumeratorLists because:
  12.  * - unnamed EnumeratorLists can't go in the symbol table.
  13.  * - a single EnumeratorList can be typedef'ed or enum'ed to more than one
  14.  *   symbol, in which case it is referenced from the typedef_table or
  15.  *   enum_table repectively more than once.
  16.  */
  17. EnumeratorList *first_list = NULL, **last_next_list = &first_list;
  18.  
  19. /* Initialize a list of enumerators.*/
  20. EnumeratorList *
  21. new_enumerator_list (enumerator)
  22.      Enumerator *enumerator;
  23. {
  24.     Enumerator *p;
  25.     EnumeratorList *list;
  26.     
  27.     list = (EnumeratorList *)safe_malloc(sizeof *list);
  28.     *last_next_list = list;
  29.     last_next_list = &list->next;
  30.     list->next = NULL;
  31.     
  32.     p = (Enumerator *)safe_malloc(sizeof(Enumerator));
  33.     *p = *enumerator;
  34.     
  35.     list->first = list->last = p;
  36.     p->next = NULL;
  37.     return list;
  38. }
  39.  
  40. /* Add the enumerator to the list. */
  41. void
  42. add_enumerator_list (list, enumerator)
  43.      EnumeratorList *list;
  44.      Enumerator *enumerator;
  45. {
  46.     Enumerator *p;
  47.  
  48.     p = (Enumerator *)safe_malloc((unsigned)sizeof(Enumerator));
  49.     *p = *enumerator;
  50.  
  51.     list->last->next = p;
  52.     list->last = p;
  53.     p->next = NULL;
  54. }
  55.  
  56. /* Free storage used by the elements in the enumerator list. */
  57. void
  58. free_enumerator_list (enumerator_list)
  59.      EnumeratorList *enumerator_list;
  60. {
  61.     Enumerator *p, *next;
  62.  
  63.     p = enumerator_list->first;
  64.     while (p != NULL) {
  65.     next = p->next;
  66.     free_enumerator(p);
  67.     free(p);
  68.     p = next;
  69.     }
  70. }
  71.  
  72.  
  73. void
  74. new_enumerator(e, name, comment_before, comment_after)
  75.      Enumerator *e;
  76.      char *name;
  77.      char *comment_before;
  78.      char *comment_after;
  79. {
  80.     e-> name = name;
  81.     e-> comment = comment_after ? comment_after : comment_before;
  82.     e-> group_comment = comment_before && comment_after ? comment_before : NULL;
  83. }
  84.  
  85. /* Free the storage used by the enumerator.*/
  86. void
  87. free_enumerator (param)
  88.      Enumerator *param;
  89. {
  90.     free(param->name);
  91.     safe_free(param->comment);
  92.     safe_free(param->group_comment);
  93. }
  94.  
  95. /* add a comment to the last enumerator in the list */
  96. int
  97. comment_last_enumerator(list, comment)
  98.      EnumeratorList *list;
  99.      char *comment;
  100. {
  101.     if (list->last->comment)
  102.     {
  103.     if (list->last->group_comment)
  104.     {
  105.         enumerator_error(list->last->name);
  106.         free(comment);
  107.         return 0;
  108.     }
  109.  
  110.     list->last->group_comment = list->last->comment;
  111.     }
  112.  
  113.     list->last->comment = comment;
  114.     return 1;
  115. }
  116.  
  117. /* enum namespace management */
  118. void add_enum_symbol(name, enum_list)
  119.      char *name;
  120.      EnumeratorList *enum_list;
  121. {
  122.     Symbol *entry = new_symbol(enum_table, name, DS_NONE);
  123.     
  124.     if (entry)
  125.     {
  126.     entry->value.enum_list = enum_list;
  127.     entry->valtype = SYMVAL_ENUM;
  128.     }
  129. }
  130.  
  131. /* look for the Enumerator list associated with the symbol */
  132. EnumeratorList *find_enum_symbol(name)
  133.      char *name;
  134. {
  135.     Symbol *entry = find_symbol(enum_table, name);
  136.     
  137.     if (entry)
  138.         return entry->value.enum_list;
  139.     else
  140.         return NULL;
  141. }
  142.  
  143. void destroy_enum_lists()
  144. {
  145.     EnumeratorList *list, *next;
  146.     
  147.     /* free all the enumerator lists */
  148.     for (list = first_list; list; list = next)
  149.     {
  150.     next = list->next;
  151.     free_enumerator_list(list);
  152.     free(list);
  153.     }
  154. }
  155.  
  156. /* create new typedef symbols */
  157. void new_typedef_symbols(decl_spec, decl_list)
  158.      DeclSpec *decl_spec;
  159.      DeclaratorList *decl_list;
  160. {
  161.     Declarator *d;
  162.     
  163.     for (d = decl_list->first; d; d = d-> next)
  164.     {
  165.     Symbol *s = new_symbol(typedef_names, d->name, DS_NONE);
  166.     
  167.     if (s && decl_spec->enum_list)
  168.     {
  169.         s->value.enum_list = decl_spec->enum_list;
  170.         s->valtype = SYMVAL_ENUM;
  171.     }
  172.     }
  173. }
  174.  
  175. void enumerator_error(name)
  176.      char *name;
  177. {
  178.     yyerror("enumerator '%s' has multiple comments", name);
  179. }
  180.